package me.hunche.app.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.hunche.app.dao.ActivityDao;
import me.hunche.app.dao.ActivityMemberDao;
import me.hunche.app.dao.TeamDao;
import me.hunche.app.dao.UserDao;
import me.hunche.app.domain.Activity;
import me.hunche.app.domain.ActivityMember;
import me.hunche.app.service.ActivityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.feinno.framework.common.dao.support.PageInfo;
import com.feinno.framework.common.service.EntityServiceImpl;
import com.hp.hpl.helper.misc.DateHelper;

@Service("activityService")
public class ActivityServiceImpl extends EntityServiceImpl<Activity, ActivityDao> implements ActivityService {
	
	@Autowired ActivityMemberDao amd;
	@Autowired UserDao ud;
	@Autowired TeamDao td;
	
	public Activity findActivityById(Long activityId) {
		Activity a = getEntityDao().get(activityId);
		if(a!=null){
			load(a);
		}
		return a;
	}

	public List<Activity> findByActivityDateAndStatus(Date d, int statusVaild) {
		 List<Activity> as =getEntityDao().findByActivityDateAndStatus(d, statusVaild);
		 if(as!=null && !as.isEmpty()){
			 for(Activity a:as){
				 load(a);
			 }
		 }
		return as;
	}

	public Activity createActivity(Date d, String line, String rendezvous,Date rendezvousTime) {
		return null;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked",  })
	protected void load(Activity a) {
		if(a.getTeamId()!=null){
			a.setTeam(td.get(a.getTeamId()));
		}
		
		List<ActivityMember> as = amd.findByActivityId(a.getId());
		
		a.setAllMembers(null);
		a.setMembers(null);
		a.setReserveMembers(null);
		
		if(as!=null && !as.isEmpty()){
			for(ActivityMember am:as){
//				load(am);
				if(ActivityMember.TYPE_REGULAR==am.getType()){
					List as1 = a.getMembers();
					if(as1==null){
						as1 =new ArrayList();
						a.setMembers(as1);
					}
					as1.add(am);
				}else if(ActivityMember.TYPE_RESERVE==am.getType()){
					List as2 = a.getReserveMembers();
					if(as2==null){
						as2 =new ArrayList();
//						a.setBackupMembers(as2);
						a.setReserveMembers(as2);
					}
					as2.add(am);
				}else if(ActivityMember.TYPE_LEADER==am.getType()){
					a.setLeader(am);
				}
			}
			a.setAllMembers(as);
		}
	}
	
	protected void load(ActivityMember am) {
		am.setUser(ud.get(am.getUserId()));
	}

	public Activity createActivity(Activity activity) {
		activity.setCreateDate(DateHelper.now());
		activity.setStatus(Activity.STATUS_NEW);
		return activity;
	}

	public List<Activity> findRecruitActivityByTeamId(Long teamId) {
		return null;
	}

	public List<Activity> findRecruitActivitys() {
		return getEntityDao().findByStatus(Activity.STATUS_RECRUIT);
	}

	public List<Activity> findReadyActivitys() {
		return getEntityDao().findByStatus(Activity.STATUS_PREP);
	}

//	public List<Activity> findGoingActivitys() {
//		return getEntityDao().findByStatus(Activity.STATUS_GOING);
//	}

	public List<Activity> findOverActivitys() {
		return getEntityDao().findByStatus(Activity.STATUS_OVER);
	}

	public List<Activity> getLatestActivitys(Long teamId,Integer maxResults) {
//		return getEntityDao().findLatestByTeamId(teamId, maxResults);
		PageInfo<Activity> pageInfo= new  PageInfo();
		pageInfo.setCountOfCurrentPage(maxResults);
		Map<String, Object> map = new HashMap();
		map.put("search_EQ_teamId", teamId);
		Map<String, Boolean> orderMap = new HashMap(); 
		PageInfo<Activity> pageInfo2 = query(pageInfo, map, orderMap);
		return pageInfo2.getPageResults();
	}

	public List<Activity> findByPage(Long teamId, Long maxId, Integer sinceId,Integer count) {
		// TODO Auto-generated method stub
		return null;
	}


}
